package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.model.UserData;
import com.jp.knowledge.my.fragment.InvitePeopleFragment;
import com.jp.knowledge.my.model.OrganizaModel;
import com.jp.knowledge.view.TabView;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/7/4.
 */

public class InvitePeopleActivity extends SlidingActivity {

	@ViewInject(R.id.data_show_view)
	private LinearLayout dataShowView;
	@ViewInject(R.id.tab_view)
	private TabView tabView;
	@ViewInject(R.id.review_viewpager)
	private ViewPager viewPager;
	@ViewInject(R.id.no_review_view)
	private LinearLayout noReviewView;
	@ViewInject(R.id.audit_join_Tv)
	private TextView noViewText;
	@ViewInject(R.id.refresh)
	private CanRefreshLayout refreshLayout;

	private UserData userData;
	private List<Fragment> fragments;
	private List<OrganizaModel> organizeList;
	private List<String> tabNames;

	@Override
	protected int getResourceId() {
		return R.layout.activity_review_organize;
	}

	@Override
	protected void init() {
		initData();
		initTitle();
		initView();
		setViewVisibility();
		scrollCompany(getIntent().getStringExtra("companyId"));
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		scrollCompany(intent.getStringExtra("companyId"));
	}

	private void scrollCompany(String companyId){
		if(companyId == null || companyId.trim().length() == 0 || organizeList == null || organizeList.size() == 0)
			return;
		for(int i = 0; i < organizeList.size(); i++){
			if(companyId.equals(organizeList.get(i).getCompanyId())){
				viewPager.setCurrentItem(i);
				return;
			}
		}
	}

	private void initData(){
		userData = application.getUserData();
		tabNames = new ArrayList<>();
		fragments = new ArrayList<>();
		organizeList = getCompanys();
		for(int i = 0, j = organizeList.size(); i < j; i++) {
			OrganizaModel organizaModel = organizeList.get(i);
			String tabName = organizaModel.getCompanyName();
			if(tabName == null)
				tabName = "";
			tabNames.add(tabName);
			fragments.add(new InvitePeopleFragment().getInstance(userData.getUsername(), organizaModel));
		}
	}

	protected  List<OrganizaModel> getCompanys(){
		List<OrganizaModel> companyList = new ArrayList<>();
		if(userData == null)
			return companyList;
		List<OrganizaModel> allCompanyList = userData.getCompanyList();
		if(allCompanyList != null && allCompanyList.size() > 0) {
			for (OrganizaModel company : allCompanyList){
				if(company.getCompanyState() != 1 || (company.getAuditStete() != 0 && company.getAuditStete() != 2))
					companyList.add(company);
			}
		}
		return  companyList;
	}

	private void initTitle(){
		topName.setText("邀请成员");
		rightIcon.setVisibility(View.GONE);
		leftIcon.setImageResource(R.mipmap.guanzhufanhui);
		leftIcon.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
	}

	private void initView(){
		refreshLayout.setVisibility(View.GONE);
		noViewText.setText("尚未加入任何组织!");
		viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
			@Override
			public Fragment getItem(int position) {
				return fragments.get(position);
			}
			@Override
			public int getCount() {
				return fragments.size();
			}
		});
		if(tabNames.size() > 0)
			tabView.setupWithViewPager(viewPager);
		tabView.setTabItems(tabNames);
	}

	private void setViewVisibility(){
		if(fragments.size() == 0){
			dataShowView.setVisibility(View.GONE);
			tabView.setVisibility(View.GONE);
			noReviewView.setVisibility(View.VISIBLE);
		} else{
			dataShowView.setVisibility(View.VISIBLE);
			noReviewView.setVisibility(View.GONE);
			tabView.setVisibility(fragments.size() == 1 ? View.GONE : View.VISIBLE);
		}
	}
}
